import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

class SPUtils {
  static const String _LANG_KEY = "lang_key";


  static late final SharedPreferences _sp;

  //uuid
  static String _uuid = "unknown";

  static get uuid {
    return _uuid;
  }

  static init() async {
    _sp = await SharedPreferences.getInstance();

    var asString = getAsString("uuid");
    if (asString.isEmpty) {
      _uuid = const Uuid().v1();
      setAsString("uuid", _uuid);
    } else {
      _uuid = asString;
    }
  }

  ///设置语言
  static setLangCode(String langCode) {
    setAsString(_LANG_KEY, langCode);
  }

  ///移除key
  static remove(String key) {
    _sp.remove(key);
  }

  ///获取语言code
  static String getLangCode([String defaultValue = "en"]) {
    return getAsString(_LANG_KEY, defaultValue: defaultValue);
  }

  static setAsString(String key, String value) {
    _sp.setString(key, value);
  }

  static setAsInt(String key, int value) {
    _sp.setInt(key, value);
  }

  static String getAsString(String key, {String defaultValue = ""}) {
    return _sp.getString(key) ?? defaultValue;
  }

  static int getAsInt(String key, {int defaultValue = 0}) {
    return _sp.getInt(key) ?? defaultValue;
  }
}
